/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.ui;

import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextField;

import lineage.ui.customize.L1Font;
import lineage.util.Util;

/**
 * 单行文本框.
 * 
 * @version 2014年2月11日下午5:13:56
 * @author jrwz
 */
public class L1TextField extends JTextField implements FocusListener, KeyListener {
    private static final long serialVersionUID = -1711593239383072604L;
    /** 字体大小(默认13). */
    protected float fontSize = 13.0f;
    /** 组件名称. */
    protected String name;
    /** 组件的x坐标. */
    protected int _x;
    /** 组件的y坐标. */
    protected int _y;
    /** 组件的宽度. */
    protected int _width;
    /** 组件的高度. */
    protected int _height;
    /** 预设要显示的内容. */
    protected String presetText;

    protected L1TextField() {
    }

    /**
     * 初始化.
     * 如要添加其他组件,应在此方法之前.
     * 
     * @param flag
     *            - 是否立即显示
     */
    protected void init(boolean flag) {
        setLayout();
        setFont();
        setName(name); // 设置组件的名称(标识符)
        setBounds(_x, _y, _width, _height);
        addListener(); // 增加监听器
        setText(presetText);
        setVisible(flag);
    }

    /**
     * 设置布局(默认空布局).
     */
    protected void setLayout() {
        setLayout(null); // 设置空布局(其上的组件可以任意调整位置)
    }

    /**
     * 设置字体(默认微软雅黑13号).
     */
    protected void setFont() {
        L1Font.setFont(Font.PLAIN, fontSize);
        setFont(L1Font.getFont()); // 设置字体
    }

    /**
     * 增加监听器(默认增加键盘事件监听与焦点状态监听).
     */
    protected void addListener() {
        addKeyListener(this); // 增加键盘事件监听
        addFocusListener(this); // 增加焦点状态监听
    }

    /**
     * 取得预先要显示的字符串.
     * 
     * @return 预先要显示的字符串内容
     */
    public String getPresetText() {
        return presetText;
    }

    /**
     * 设置预先要显示的字符串.
     * 
     * @param s
     *            - 预先要显示的字符串
     */
    public void setPresetText(String s) {
        presetText = s;
    }

    // 得到焦点
    @Override
    public void focusGained(FocusEvent e) {
        if (getText().equalsIgnoreCase(getPresetText())) {
            setText(""); // 清空
        }
    }

    // 失去焦点
    @Override
    public void focusLost(FocusEvent e) {
        if (getText().equalsIgnoreCase("")) {
            setText(getPresetText()); // 写入预设字符串
        }
    }

    // 当键盘输入时调用
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO 自动生成的方法存根

    }

    // 当键盘按键被按下时调用
    @Override
    public void keyPressed(KeyEvent e) {
        // TODO 自动生成的方法存根

    }

    // 当键盘按键松开时调用
    @Override
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_ENTER: // 按下回车键
            Util.message("当前文本框：" + getName() + "--->>输入的内容为：" + getText());
            setText("");
            break;
        default:
            break;
        }
    }
}
